#include "matrix_keyboard.h"
#include "c51.h"
#include "utils.h"

/**
 * 逐列再逐行扫描
 */
u8 matrix_column_row_key(void) {
  u8 keyboard_num = 0;
  // 第一列
  P1 = 0xFF;
  P1_3 = 0;
  if (P1_7 == 0) {
    delay(20);
    while (P1_7 == 0)
      ;
    delay(20);
    keyboard_num = 1;
  }

  if (P1_6 == 0) {
    delay(20);
    while (P1_6 == 0)
      ;
    delay(20);
    keyboard_num = 5;
  }

  if (P1_5 == 0) {
    delay(20);
    while (P1_5 == 0)
      ;
    delay(20);
    keyboard_num = 9;
  }

  if (P1_4 == 0) {
    delay(20);
    while (P1_4 == 0)
      ;
    delay(20);
    keyboard_num = 13;
  }

  // 第二列
  P1 = 0xFF;
  P1_2 = 0;
  if (P1_7 == 0) {
    delay(20);
    while (P1_7 == 0)
      ;
    delay(20);
    keyboard_num = 2;
  }

  if (P1_6 == 0) {
    delay(20);
    while (P1_6 == 0)
      ;
    delay(20);
    keyboard_num = 6;
  }

  if (P1_5 == 0) {
    delay(20);
    while (P1_5 == 0)
      ;
    delay(20);
    keyboard_num = 10;
  }

  if (P1_4 == 0) {
    delay(20);
    while (P1_4 == 0)
      ;
    delay(20);
    keyboard_num = 14;
  }

  // 第三列
  P1 = 0xFF;
  P1_1 = 0;
  if (P1_7 == 0) {
    delay(20);
    while (P1_7 == 0)
      ;
    delay(20);
    keyboard_num = 3;
  }

  if (P1_6 == 0) {
    delay(20);
    while (P1_6 == 0)
      ;
    delay(20);
    keyboard_num = 7;
  }

  if (P1_5 == 0) {
    delay(20);
    while (P1_5 == 0)
      ;
    delay(20);
    keyboard_num = 11;
  }

  if (P1_4 == 0) {
    delay(20);
    while (P1_4 == 0)
      ;
    delay(20);
    keyboard_num = 15;
  }

  // 第四列
  P1 = 0xFF;
  P1_0 = 0;
  if (P1_7 == 0) {
    delay(20);
    while (P1_7 == 0)
      ;
    delay(20);
    keyboard_num = 4;
  }

  if (P1_6 == 0) {
    delay(20);
    while (P1_6 == 0)
      ;
    delay(20);
    keyboard_num = 8;
  }

  if (P1_5 == 0) {
    delay(20);
    while (P1_5 == 0)
      ;
    delay(20);
    keyboard_num = 12;
  }

  if (P1_4 == 0) {
    delay(20);
    while (P1_4 == 0)
      ;
    delay(20);
    keyboard_num = 16;
  }

  return keyboard_num;
}

/**
 * 逐行再逐列扫描
 */
u8 matrix_row_column_key(void) {
  u8 keyboard_num = 0;
  // 第一行
  P1 = 0xFF;
  P1_7 = 0;
  if (P1_3 == 0) {
    delay(20);
    while (P1_3 == 0)
      ;
    delay(20);
    keyboard_num = 1;
  }

  if (P1_2 == 0) {
    delay(20);
    while (P1_2 == 0)
      ;
    delay(20);
    keyboard_num = 2;
  }

  if (P1_1 == 0) {
    delay(20);
    while (P1_1 == 0)
      ;
    delay(20);
    keyboard_num = 3;
  }

  if (P1_0 == 0) {
    delay(20);
    while (P1_0 == 0)
      ;
    delay(20);
    keyboard_num = 4;
  }

  // 第二行
  P1 = 0xFF;
  P1_6 = 0;
  if (P1_3 == 0) {
    delay(20);
    while (P1_3 == 0)
      ;
    delay(20);
    keyboard_num = 5;
  }

  if (P1_2 == 0) {
    delay(20);
    while (P1_2 == 0)
      ;
    delay(20);
    keyboard_num = 6;
  }

  if (P1_1 == 0) {
    delay(20);
    while (P1_1 == 0)
      ;
    delay(20);
    keyboard_num = 7;
  }

  if (P1_0 == 0) {
    delay(20);
    while (P1_0 == 0)
      ;
    delay(20);
    keyboard_num = 8;
  }

  // 第三行
  P1 = 0xFF;
  P1_5 = 0;
  if (P1_3 == 0) {
    delay(20);
    while (P1_3 == 0)
      ;
    delay(20);
    keyboard_num = 9;
  }

  if (P1_2 == 0) {
    delay(20);
    while (P1_2 == 0)
      ;
    delay(20);
    keyboard_num = 10;
  }

  if (P1_1 == 0) {
    delay(20);
    while (P1_1 == 0)
      ;
    delay(20);
    keyboard_num = 11;
  }

  if (P1_0 == 0) {
    delay(20);
    while (P1_0 == 0)
      ;
    delay(20);
    keyboard_num = 12;
  }

  // 第四行
  P1 = 0xFF;
  P1_4 = 0;
  if (P1_3 == 0) {
    delay(20);
    while (P1_3 == 0)
      ;
    delay(20);
    keyboard_num = 13;
  }

  if (P1_2 == 0) {
    delay(20);
    while (P1_2 == 0)
      ;
    delay(20);
    keyboard_num = 14;
  }

  if (P1_1 == 0) {
    delay(20);
    while (P1_1 == 0)
      ;
    delay(20);
    keyboard_num = 15;
  }

  if (P1_0 == 0) {
    delay(20);
    while (P1_0 == 0)
      ;
    delay(20);
    keyboard_num = 16;
  }

  return keyboard_num;
}
